package cs1;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class Myclass {

    private String name;
    private int age;

    public Myclass() {
    }

    public Myclass(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private Myclass(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Myclass{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @InitMethod
    public void init() {
        System.out.println("init()");
    }

    public static void main(String[] args) {
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = Myclass.class.getResourceAsStream("/myapp.properties");
            if (inputStream == null) return;
            properties.load(inputStream);
            //读取myapp.properties属性文件
            String name = properties.getProperty("Class");
            //根据属性文件中配置的类名创建一个对象,使用反射的方式
            Class<Myclass> myClass = Myclass.class;

            Constructor<Myclass> constructor = null;
            try {
                constructor = myClass.getConstructor(String.class, int.class);
                constructor.setAccessible(true);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }

            Myclass instance=null;
            try {
                instance = constructor.newInstance("Tom", 12);
            } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }

            //通过反射找到带有自定义注解的方法
            Method init = null;//带有自定义注解的方法
            Method[] methods = myClass.getDeclaredMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(InitMethod.class)) {
                    init = method;
                    break;
                }
            }
            //调用该方法
            if (init != null) {
                init.setAccessible(true);
                try {
                    init.invoke(instance);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
            } else {
                throw new RuntimeException("没有找到带有InitMethod注解的方法!");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //流的关闭操作
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
